package com.skm.open.sensitive.word.processor;

import com.skm.open.sensitive.word.processor.autoconfigure.SensitiveWordProcessorProperties;
import com.skm.open.sensitive.word.processor.entity.KeyWord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import java.io.InputStream;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author Skqing
 * @date 2021/7/22 20:14
 * @desc 敏感词处理
 */

public class SensitiveWordProcessor {
    private static final Logger LOGGER = LoggerFactory.getLogger(SensitiveWordProcessor.class);

    private SensitiveWordProcessorProperties sensitiveWordProcessorProperties;

    /**
     * 敏感词模块. key为模块名，value为对应的敏感词搜索器
     */
    private Map<String, KWSeeker> seekers = new ConcurrentHashMap<>(8);

    public SensitiveWordProcessor(SensitiveWordProcessorProperties sensitiveWordProcessorProperties) {
        this.sensitiveWordProcessorProperties = sensitiveWordProcessorProperties;

        if (!StringUtils.hasText(sensitiveWordProcessorProperties.getFilePath())) {
            LOGGER.warn("config file path is null");
            sensitiveWordProcessorProperties.setFilePath("sensitive-word.properties");
        }
        initConfig();
    }

    private void initConfig() {
        try (InputStream in = getClass().getClassLoader().getResourceAsStream(sensitiveWordProcessorProperties.getFilePath())) {
            Properties prop = new Properties();
            prop.load(in);
            Set<Object> keySet = prop.keySet();
            for (Object key : keySet) {
                Object value = prop.get(key);
                if (Objects.isNull(value)) {
                    LOGGER.warn("key {} has no value", key);
                    continue;
                }
                String[] words = String.valueOf(value).split(",");
                if (words == null || words.length == 0) {
                    LOGGER.warn("key {} has no value array", key);
                    continue;
                }
                Set<KeyWord> kws = new HashSet<>();
                for (String w : words) {
                    kws.add(new KeyWord(String.valueOf(w)));
                }
                seekers.put(String.valueOf(key), new KWSeeker(kws));
            }
        } catch (Exception e) {
            LOGGER.warn("getResourceAsStream exception", e);
        }
    }

    /**
     * 获取一个敏感词搜索器
     *
     * @param wordType
     * @return
     */
    public KWSeeker getKWSeeker(String wordType) {
        return seekers.get(wordType);
    }

    /**
     * 加入一个敏感词搜索器
     *
     * @param wordType
     * @param kwSeeker
     * @return
     */
    public void putKWSeeker(String wordType, KWSeeker kwSeeker) {
        seekers.put(wordType, kwSeeker);
    }

    /**
     * 移除一个敏感词搜索器
     *
     * @param wordType
     * @return
     */
    public void removeKWSeeker(String wordType) {
        seekers.remove(wordType);
    }

    /**
     * 清除空搜索器
     */
    public void clear() {
        seekers.clear();
    }
}